<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8"/>
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
		<meta name="HandheldFriendly" content="true"/>
		<meta name="MobileOptimized" content="320"/>
		<title>Hello H5+</title>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript">
/*	
NSNotificationCenter *nc = nil;

// 游戏玩家登录状态监听函数
- (void)authenticationChanged:(NSNotification*)notification
{
    // 获取游戏玩家共享实例对象
    GKLocalPlayer *player = notification.object;
    if ( player.isAuthenticated ) {
        // 玩家已登录认证，获取玩家信息
        [self playerInformation:player];
    } else {
        // 玩家未登录认证，提示用户登录
        NSLog(@"请登录！");
    }
    // 释放使用的对象
    [player release];
}

// 获取游戏玩家状态信息
- (void)playerInformation:(GKPlayer *)player
{
    // 获取游戏玩家的名称
    NSLog(@"Name: %@",player.displayName);
}

// 登录到游戏中心
- (void)loginGamecenter
{
    // 获取游戏玩家共享实例对象
    GKLocalPlayer *localplayer = [GKLocalPlayer localPlayer];
    // 判断游戏玩家是否已经登录认证
    if ( localplayer.isAuthenticated ) {
        // 玩家已登录认证，获取玩家信息
        [self playerInformation:localplayer];
    } else {
	    // 监听用户登录状态变更事件
	    if(!nc){
		    nc = [NSNotificationCenter defaultCenter];
		    [nc addObserver:self
		           selector:@selector(authenticationChanged)
		               name:@"GKPlayerAuthenticationDidChangeNotificationName"
		             object:nil];
		}
        // 玩家未登录认证，发起认证请求
        [localplayer authenticateWithCompletionHandler:nil];
        NSLog(@"登录中...");
    }
	// 释放使用的对象
    [localplayer release];
}

// 停止监听登录游戏状态变化
- (void)logoutGamecenter
{
    // 取消监听用户登录状态变化
    if(nc){
	    [nc removeObserver:self
	                  name:@"GKPlayerAuthenticationDidChangeNotificationName"
	                object:nil];
	}
}
*/

// H5 plus事件处理
var GKLocalPlayer=null,NSNotificationCenter=null;
var nc=null,delegate=null;
function plusReady(){
	plus.webview.currentWebview().setStyle({popGesture:"none"});
	if ( plus.os.name == "iOS" ) {
		GKLocalPlayer  = plus.ios.importClass("GKLocalPlayer");
		NSNotificationCenter = plus.ios.importClass("NSNotificationCenter");
		longinGamecenter();
	} else {
		eStart.innerText = "欢迎您";
		bLogin = true;
		setTimeout( function(){
			plus.nativeUI.toast( "此平台不支持Game Center功能！" );
		}, 500 );
	}
}
if(window.plus){
	plusReady();
}else{
	document.addEventListener("plusready",plusReady,false);
}
// DOMContentLoaded事件处理
var eStart=null,bLogin=false;
document.addEventListener("DOMContentLoaded",function(){
	eStart = document.getElementById( "start" );
},false);

// 游戏玩家登录状态监听函数
function authenticationChanged( notification ){
	// 获取游戏玩家共享实例对象
	var player = notification.plusGetAttribute("object");
	if ( player.plusGetAttribute("isAuthenticated") ) {
		// 玩家已登录认证，获取玩家信息
		playerInformation(player);
		bLogin = true;
	} else {
		// 玩家未登录认证，提示用户登录
		setTimeout(function(){
			eStart.innerText = "请登录";
	        bLogin = false;
		},500);
	}
	// 释放使用的对象
//	plus.ios.deleteObject(player);
}

// 获取游戏玩家状态信息
function playerInformation( player ){
	var name = player.plusGetAttribute("displayName");
	eStart.innerText = name+" 已登录！";
}

// 登录到游戏中心
function longinGamecenter(){
	if ( bLogin ){
		return;
	}
    // 获取游戏玩家共享实例对象
    var localplayer = GKLocalPlayer.localPlayer();
    // 判断游戏玩家是否已经登录认证
    if ( localplayer.plusGetAttribute("isAuthenticated") ) {
        // 玩家已登录认证，获取玩家信息
        playerInformation( localplayer );
        bLogin = true;
    } else {
        eStart.innerText = "登录中...";
		// 监听用户登录状态变更事件
	    if(!nc){
	    	nc = NSNotificationCenter.defaultCenter();
	    	delegate||(delegate=plus.ios.implements("NSObject",{"authenticationChanged:":authenticationChanged}));
	    	nc.addObserverselectornameobject(delegate,
		    	plus.ios.newObject("@selector","authenticationChanged:"),
		    	"GKPlayerAuthenticationDidChangeNotificationName",
		    	null);
	    }
        // 玩家未登录认证，发起认证请求
        localplayer.authenticateWithCompletionHandler(null);
    }
    // 释放使用的对象
	plus.ios.deleteObject(localplayer);
}

// 停止监听登录游戏状态变化
function stopGamecenterObserver()
{
    // 取消监听用户登录状态变化
    nc&&nc.removeObservernameobject(delegate,"GKPlayerAuthenticationDidChangeNotificationName",null);
    plus.ios.deleteObject(nc);nc = null;
    plus.ios.deleteObject(delegate);delegate = null;
}
		</script>
		<style type="text/css">
* {
	-webkit-user-select: none;
	-ms-touch-select: none;
}
html {
	width: 100%;
	height: 100%;
}
body {
	margin: 0;
	padding: 0;
	width: 100%;
	height: 100%;
	text-align: center;
	-webkit-touch-callout:none;
	-webkit-tap-highlight-color:rgba(0,0,0,0);
}
header {
	height: 44px;
	font-size: 22px;
	line-height: 44px;
	font-weight: bold;
	color: #000000;
}
.game {
	position:fixed;
	top:44px;
	width:100%;
	bottom: 60px;
	background: no-repeat center center url(../img/5.jpg);
}
.button {
	font-size: 18px;
	font-weight: normal;
	text-decoration: none;
	display: block;
	text-align: center;
	color: #fff;
	background-color: #FFCC33;
	border: 1px solid #ECB100;
	padding: .5em 0em;
	margin: .5em .7em;
	-webkit-border-radius: 5px;
	border-radius: 5px;
}
.button:active {
	outline: 0;
	-webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
	box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
}
.start {
	width: 50%;
	font-size: 18px;
	font-weight: normal;
	text-decoration: none;
	display: block;
	text-align: center;
	color: #666;
	background-color: #EBEBEB;
	border: 1px solid #E0E0E0;
	padding: .5em 0em;
	margin: .5em auto;
	-webkit-border-radius: 5px;
	border-radius: 5px;
}
		</style>
		<style type="text/css" media="screen and (min-aspect-ratio: 1/1)">
.autobg {
	background-size: auto 100%;
}
		</style>
		<style type="text/css" media="screen and (max-aspect-ratio: 1/1)">
.autobg {
	background-size: 100% auto;
}
		</style>
	</head>
	<body style="background:#CCCCCC;">
		<header id=header>五子棋</header>
		<div class="game autobg">
		</div>
		<div style="position:fixed;bottom:0;width:100%;height:60px;text-align:center;">
			<div class="button" onclick="try{stopGamecenterObserver();}catch(e){}back();">退出</div>
		</div>
		<div style="position:fixed;top:30%;width:100%;text-align:center;">
			<div id="start" class="start" onclick="longinGamecenter();">登录中...</div>
		</div>
	</body>
	<script type="text/javascript" src="../js/immersed.js" ></script>
</html>